package com.test.touch;
import com.handinfo.engine.AbstractGameScreen;
import com.handinfo.uimenu.UIAbstractWindow;
import com.handinfo.uimenu.UIManager;
import com.handinfo.util.ArrayList;

public class GameTouchManager {
	public static final int MAX = 2+10+2+1;
	private static ArrayList stouchRect = new ArrayList(MAX,2);
	private static ArrayList stouchRectBackup = new ArrayList(MAX,2);
//	private static int current = -1;
//	private static TouchWin mcurrent;
	
	/**
	 * 非window使用。
	 * @param rect
	 */
	public static void addStaticRect(TouchRect rect) {
		if (!stouchRect.contain(rect)) {
			stouchRect.addElement(rect);
		}
	}
	
//	public static void setTouchWin(TouchWin obj) {
//		mcurrent = obj;
//	}
	
	public static void clearStaticRects() {
		stouchRect.removeAllElements();
//		mcurrent = null;
	}
	
	/**
	 * 先检测window的rect，如果有window，不管有没有触摸矩形，都返回，
	 * 如果没有window再检测下面的静态注册的rect。
	 * @param x
	 * @param y
	 */
	public static void checkPressed(int x, int y) {
		UIAbstractWindow window = UIManager.getInstance().getCurFocusedWindow();
		if (window != null) {
			checkTouch(window.getRects(), x, y);
			return;
		}
		checkTouch(stouchRect, x, y);
	}
	
	private static void checkTouch(ArrayList rects, int x, int y) {
		int len = rects.size();
		for(int i=0;i < len;i ++) {			
			TouchRect rect = (TouchRect) rects.elementAt(i);
			if (rect != null && rect.onTouch(x, y)) {
				break;
			}
		}
	}
	
	
	public static void backUpRect() {
		stouchRectBackup.removeAllElements();
		int len = stouchRect.size();
		for(int i=0;i < len;i ++) {
			TouchRect rect = (TouchRect) stouchRect.elementAt(i);
			if (rect != null) {
				stouchRectBackup.addElement(rect);
			}
		}
		stouchRect.removeAllElements();
	}
	
	public static void backUpRect(ArrayList backfile) {
		backfile.removeAllElements();
		int len = stouchRect.size();
		for(int i=0;i < len;i ++) {
			TouchRect rect = (TouchRect) stouchRect.elementAt(i);
			if (rect != null) {
				backfile.addElement(rect);
			}
		}
//		stouchRect.removeAllElements();
	}
	
	public static void recoverRect() {
		stouchRect.removeAllElements();
		int len = stouchRectBackup.size();
		if (len ==0) {
			try {
				throw new Exception("还原按键，但是没有备份。");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		for(int i=0;i < len;i ++) {
			TouchRect rect = (TouchRect) stouchRectBackup.elementAt(i);
			if (rect != null) {
				stouchRect.addElement(rect);
			}
		}
		stouchRectBackup.removeAllElements();
	}
	
	public static void recoverRect(ArrayList backfile) {
		stouchRect.removeAllElements();
		int len = backfile.size();
		if (len ==0) {
			try {
				throw new Exception("还原按键，但是没有备份。");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		for(int i=0;i < len;i ++) {
			TouchRect rect = (TouchRect) backfile.elementAt(i);
			if (rect != null) {
				stouchRect.addElement(rect);
			}
		}
//		backfile.removeAllElements();
	}

//	public static final void registerSoftRL() {
//		registerNormalSoft(true, true);
//	}
	
	public static void registerStaticSoftKey(boolean left, boolean right) {
		if (left) {
			addStaticRect(new TouchRect(TouchDefines.getLeftSoftRect(), new TouchListener() {
				
				public void doTouch(int x, int y) {
					AbstractGameScreen.s_instance.keyPressed(AbstractGameScreen.LEFT_SOFT);
				}
			}));
		}
		if (right) {
			addStaticRect(new TouchRect(TouchDefines.getRightSoftRect(), new TouchListener() {
				
				public void doTouch(int x, int y) {
					AbstractGameScreen.s_instance.keyPressed(AbstractGameScreen.RIGHT_SOFT);
				}
			}));
		}
	}
	
	public static final void keyPressedFire() {
		AbstractGameScreen.s_instance.keyPressed(AbstractGameScreen.MIDDLE);
	}
	public static final void keyPressedLSoft() {
		AbstractGameScreen.s_instance.keyPressed(AbstractGameScreen.LEFT_SOFT);
	}
	public static final void keyPressedStar() {
		AbstractGameScreen.s_instance.keyPressed(AbstractGameScreen.KEY_STAR);
	}
	public static final void keyPressedPound() {
		AbstractGameScreen.s_instance.keyPressed(AbstractGameScreen.KEY_POUND);
	}
	public static final void keyPressedRSoft() {
		AbstractGameScreen.s_instance.keyPressed(AbstractGameScreen.RIGHT_SOFT);
	}
	
	/**
	 * @param num 0-9
	 */
	public static final void keyPressedNum(int num) {
		AbstractGameScreen.s_instance.keyPressed(AbstractGameScreen.KEY_NUM0+num);
	}
	/**
	 * @param dir 0,1,2,3,左上右下
	 */
	public static final void keyPressedDir(int dir) {
		int result = 999;
		switch(dir) {
		case 0:
			result = AbstractGameScreen.LEFT_ARROW;
			break;
		case 1:
			result = AbstractGameScreen.UP_ARROW;
			break;
		case 2:
			result = AbstractGameScreen.RIGHT_ARROW;
			break;
		case 3:
			result = AbstractGameScreen.DOWN_ARROW;
			break;
		}
		if (result != 999) {
			AbstractGameScreen.s_instance.keyPressed(result);
		}
	}
}
